package com.ccrfid.rmc6;

import com.ccrfid.rmc6.lbservice3.mq.MqMsgDev;
import com.ccrfid.rmc6.lbservice3.pojo.msg.MsgType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class MockDataGenerator {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("failover:(tcp://10.2.120.200:61616)");
        Connection connection = null;
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createTopic("mp_devices_real");
            MessageProducer messageProducer = session.createProducer(destination);
            messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            ObjectMapper objectMapper = new ObjectMapper();
            Random random = new Random();
            for (int i = 0; i < 1000; i++) {
                Integer locatorNumber = random.nextInt(840) + 1;
                List<MqMsgDev> msgList = createRandomCards(locatorNumber, 10);
                try {
                    String msgContent = objectMapper.writeValueAsString(msgList);
                    messageProducer.send(session.createTextMessage(msgContent));
                    System.out.println("mp_devices_real:" + msgContent);
                } catch (JsonProcessingException ex) {
                    ex.printStackTrace();
                }
                try {
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            messageProducer.close();
            session.close();

        } catch (JMSException ex) {
            ex.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException ex) {
                    ex.printStackTrace();
                    System.out.println("Could not close an open connection...");
                }
            }
        }
    }

    public static List<MqMsgDev> createRandomCards(Integer locatorNumber, int cardCount) {
        List<MqMsgDev> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < cardCount; i++) {
            long cardNumber = random.nextInt(1900) + 1;
            list.add(createLocateMqMsgDev(locatorNumber, cardNumber));
        }
        return list;
    }

    public static MqMsgDev createLocateMqMsgDev(Integer locatorNumber, Long cardNumber) {
        MqMsgDev mqMsg = new MqMsgDev();
        mqMsg.setLocatorNumber(locatorNumber);
        mqMsg.setCardNumber(cardNumber);
        mqMsg.setMsgType(MsgType.DEVICE_MESSAGE_TYPE_ENTERLOCATOR);
        mqMsg.setReaderNumber(0);
        mqMsg.setArg1(0);
        mqMsg.setArg2(0);
        mqMsg.setTime(new Date().getTime());
        return mqMsg;
    }

}
